<?php
class Base extends MY_Admin_Controller
{
    /**
     * 列表
     */
    public function index()
    {
        $page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$name = $this->input->get_post('name');
    	
    	$where = array();
    	if($name){
    		$where["name LIKE '%$name%'"] = NULL;
    	}
    	
        $this->load->model('common/Model_dirty_words','dirty_words');
        $total_rows = $this->dirty_words->count($where);
        
        //分页
        $this->load->library('pagination');
        
        $config['total_rows'] = $total_rows;
        $config['per_page'] = 20;
        $config['base_url'] = '/dirty_words/base/index';
        
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        
        $offset = ($page-1) * $config['per_page'];
        $list = $this->dirty_words->lists($offset,  $config['per_page'], $where);

        $agent_type = array('1'=>'收费代理', '2'=>'异业联盟');
        
        $data = array(
            'list'     => $list,
         	'show_page'   => $show_page,
         	'total_rows'  => $config['total_rows'],
        );
        
        $this->load->view('admin/dirty_words/base/index', $data);
    }
    
    /*
     * user 添加,编辑
    * */
    public function create()
    {    	
    	$this->load->view('admin/dirty_words/base/create');
    }
    
    public function save()
    {
    	$name = trim($this->input->get_post('name'));
    	
    	$data = array(
    			'name'=>$name,
    			'create_time'=>date('Y-m-d H:i:s'),
    			);
    	
    	$this->load->model('common/Model_dirty_words','dirty_words');
    	$this->dirty_words->save($data);
    	
    	$this->load->service('message/service_message_im');
    	$data = $this->service_message_im->openim_dirty_words(array($name));
    	
    	$this->load->helper('url');
    	redirect(url('admin').'dirty_words/base/index');
    }
    
    /**
     * 删除
     */
    public function delete()
    {
    	$response = array('code' => '200', 'msg' => '操作成功');
    
    	do {
    		$id = (int)$this->input->get_post('id');
    
    		if (!$id)
    		{
    			$response = error(30001);
    			break;
    		}
    		$this->load->model('common/Model_dirty_words','dirty_words');
    		
    		$row = $this->dirty_words->getById($id);
    		
    		$this->load->service('message/service_message_im');
    		$this->service_message_im->openim_dirty_words_delete(array($row['name']));
    		
    		$res = $this->dirty_words->delete(array('id' => $id));
    
    		if (!$res)
    		{
    			$response = error(10080);
    			break;
    		}
    	}while (false);
    
    
    	$this->json_output($response);
    }
}